using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using System.Web.UI.WebControls;
using System.IO;
using System.Web.UI;
using PagedList;
using PagedList.Mvc;
using MigraDoc.DocumentObjectModel;
using MigraDoc.Rendering;
using <Application>.Models;
using <Application>.Data;

namespace <Application>.Controllers
{
    public class <OwnerU>_<TableU>Controller : Controller
    {

        DataTable dt<Table> = new DataTable();
<If Loop>
     <IsRelated>
     <And>
     <IsMiscTag>
        DataTable A<MiscTag>dt<RTable> = new DataTable();
     <IsRelated>
     <And>
     <IsNotMiscTag>
        DataTable dt<RTable> = new DataTable();
</If Loop>

        // GET: /<OwnerU>_<TableU>/
        public ActionResult Index(string sortOrder,  
                                  String SearchField,
                                  String SearchCondition,
                                  String SearchText,
                                  String Export,
                                  int? PageSize,
                                  int? page, 
                                  string command)
        {

            if (command == "Show All") {
                SearchField = null;
                SearchCondition = null;
                SearchText = null;
                Session["SearchField"] = null;
                Session["SearchCondition"] = null;
                Session["SearchText"] = null; } 
            else if (command == "Add New Record") { return RedirectToAction("Create"); } 
            else if (command == "Export") { Session["Export"] = Export; } 
            else if (command == "Search" | command == "Page Size") {
                if (!string.IsNullOrEmpty(SearchText)) {
                    Session["SearchField"] = SearchField;
                    Session["SearchCondition"] = SearchCondition;
                    Session["SearchText"] = SearchText; }
                } 
            if (command == "Page Size") { Session["PageSize"] = PageSize; }

    <If Loop>
	        <IsNotText>
	        <And>
	        <IsNotNText>
            ViewData["SearchFields"] = GetFields((Session["SearchField"] == null ? "<FieldC>" : Convert.ToString(Session["SearchField"])));
        <ExitLoop>
    </If Loop>
            ViewData["SearchConditions"] = Library.GetConditions((Session["SearchCondition"] == null ? "Contains" : Convert.ToString(Session["SearchCondition"])));
            ViewData["SearchText"] = Session["SearchText"];
            ViewData["Exports"] = Library.GetExports((Session["Export"] == null ? "Pdf" : Convert.ToString(Session["Export"])));
            ViewData["PageSizes"] = Library.GetPageSizes();

            ViewData["CurrentSort"] = sortOrder;
    <If Loop>
	        <IsNotText>
	        <And>
	        <IsNotNText>
            ViewData["<FieldU>SortParm"] = sortOrder == "<FieldU>_asc" ? "<FieldU>_desc" : "<FieldU>_asc";
    </If Loop>

            dt<Table> = <OwnerU>_<TableU>Data.SelectAll();
<If Loop>
     <IsRelated>
     <And>
     <IsMiscTag>
            A<MiscTag>dt<RTable> = <OwnerU>_<TableU>_<RTableU>Data.SelectAll();
     <IsRelated>
     <And>
     <IsNotMiscTag>
            dt<RTable> = <OwnerU>_<TableU>_<RTableU>Data.SelectAll();
</If Loop>

            try
            {
                if (!string.IsNullOrEmpty(Convert.ToString(Session["SearchField"])) & !string.IsNullOrEmpty(Convert.ToString(Session["SearchCondition"])) & !string.IsNullOrEmpty(Convert.ToString(Session["SearchText"])))
                {
                    dt<Table> = <OwnerU>_<TableU>Data.Search(Convert.ToString(Session["SearchField"]), Convert.ToString(Session["SearchCondition"]), Convert.ToString(Session["SearchText"]));
                }
            }
            catch { }

            var Query = from row<Table> in dt<Table>.AsEnumerable()
<If Loop>
     <IsRelated>
     <And>
     <IsNotNull>
     <And>
     <IsMiscTag>
                        join A<MiscTag>row<RTable> in A<MiscTag>dt<RTable>.AsEnumerable() on row<Table>.Field<<VBType>>("<Field>") equals A<MiscTag>row<RTable>.Field<<VBType>>("<RField>")
     <IsRelated>
     <And>
     <IsNull>
     <And>
     <IsNotNullable>
     <And>
     <IsMiscTag>
                        join A<MiscTag>row<RTable> in A<MiscTag>dt<RTable>.AsEnumerable() on row<Table>.Field<<VBType>>("<Field>") equals A<MiscTag>row<RTable>.Field<<VBType>>("<RField>")
     <IsRelated>
     <And>
     <IsNull>
     <And>
     <IsNullable>
     <And>
     <IsMiscTag>
                        join A<MiscTag>row<RTable> in A<MiscTag>dt<RTable>.AsEnumerable() on row<Table>.Field<<VBType>?>("<Field>") equals A<MiscTag>row<RTable>.Field<<VBType>>("<RField>")
     <IsRelated>
     <And>
     <IsNotNull>
     <And>
     <IsNotMiscTag>
                        join row<RTable> in dt<RTable>.AsEnumerable() on row<Table>.Field<<VBType>>("<Field>") equals row<RTable>.Field<<VBType>>("<RField>")
     <IsRelated>
     <And>
     <IsNull>
     <And>
     <IsNotNullable>
     <And>
     <IsNotMiscTag>
                        join row<RTable> in dt<RTable>.AsEnumerable() on row<Table>.Field<<VBType>>("<Field>") equals row<RTable>.Field<<VBType>>("<RField>")
     <IsRelated>
     <And>
     <IsNull>
     <And>
     <IsNullable>
     <And>
     <IsNotMiscTag>
                        join row<RTable> in dt<RTable>.AsEnumerable() on row<Table>.Field<<VBType>?>("<Field>") equals row<RTable>.Field<<VBType>>("<RField>")
</If Loop>
                        select new <OwnerU>_<TableU>() {
<If Loop>
     <IsRelated>
     <And>
     <IsMiscTag>
     <And>
     <IsRDisplayField>
     <And>
     <IsCurRow=[0]>
                            <RTable> = new <RTable>() 
                            {
                                   <RField> = A<MiscTag>row<RTable>.Field<<VBType>>("<RField>")
                                  ,<RDisplayField> = A<MiscTag>row<RTable>.Field<<RDisplayFieldVBType>>("<RDisplayField>")
                            }
     <IsRelated>
     <And>
     <IsMiscTag>
     <And>
     <IsNotRDisplayField>
     <And>
     <IsCurRow=[0]>
                            <RTable> = new <RTable>() 
                            {
                                   <RField> = A<MiscTag>row<RTable>.Field<<VBType>>("<RField>")
                            }
     <IsRelated>
     <And>
     <IsMiscTag>
     <And>
     <IsRDisplayField>
     <And>
     <IsCurRow<>[0]>
                           ,
                            <RTable> = new <RTable>() 
                            {
                                   <RField> = A<MiscTag>row<RTable>.Field<<VBType>>("<RField>")
                                  ,<RDisplayField> = A<MiscTag>row<RTable>.Field<<RDisplayFieldVBType>>("<RDisplayField>")
                            }
     <IsRelated>
     <And>
     <IsMiscTag>
     <And>
     <IsNotRDisplayField>
     <And>
     <IsCurRow<>[0]>
                           ,
                            <RTable> = new <RTable>() 
                            {
                                   <RField> = A<MiscTag>row<RTable>.Field<<VBType>>("<RField>")
                            }
     <IsRelated>
     <And>
     <IsNotMiscTag>
     <And>
     <IsRDisplayField>
     <And>
     <IsCurRow=[0]>
                            <RTable> = new <RTable>() 
                            {
                                   <RField> = row<RTable>.Field<<VBType>>("<RField>")
                                  ,<RDisplayField> = row<RTable>.Field<<RDisplayFieldVBType>>("<RDisplayField>")
                            }
     <IsRelated>
     <And>
     <IsNotMiscTag>
     <And>
     <IsNotRDisplayField>
     <And>
     <IsCurRow=[0]>
                            <RTable> = new <RTable>() 
                            {
                                   <RField> = row<RTable>.Field<<VBType>>("<RField>")
                            }
     <IsRelated>
     <And>
     <IsNotMiscTag>
     <And>
     <IsRDisplayField>
     <And>
     <IsCurRow<>[0]>
                           ,
                            <RTable> = new <RTable>() 
                            {
                                   <RField> = row<RTable>.Field<<VBType>>("<RField>")
                                  ,<RDisplayField> = row<RTable>.Field<<RDisplayFieldVBType>>("<RDisplayField>")
                            }
     <IsRelated>
     <And>
     <IsNotMiscTag>
     <And>
     <IsNotRDisplayField>
     <And>
     <IsCurRow<>[0]>
                           ,
                            <RTable> = new <RTable>() 
                            {
                                   <RField> = row<RTable>.Field<<VBType>>("<RField>")
                            }
     <IsNotRelated>
     <And>
     <IsNotNull>
     <And>
     <IsCurRow=[0]>
                            <Field> = row<Table>.Field<<VBType>>("<Field>")
     <IsNotRelated>
     <And>
     <IsNull>
     <And>
     <IsNotNullable>
     <And>
     <IsCurRow=[0]>
                            <Field> = row<Table>.Field<<VBType>>("<Field>")
     <IsNotRelated>
     <And>
     <IsNull>
     <And>
     <IsNullable>
     <And>
     <IsCurRow=[0]>
                            <Field> = row<Table>.Field<<VBType>?>("<Field>")
     <IsNotRelated>
     <And>
     <IsNotNull>
     <And>
     <IsCurRow<>[0]>
                           ,<Field> = row<Table>.Field<<VBType>>("<Field>")
     <IsNotRelated>
     <And>
     <IsNull>
     <And>
     <IsNotNullable>
     <And>
     <IsCurRow<>[0]>
                           ,<Field> = row<Table>.Field<<VBType>>("<Field>")
     <IsNotRelated>
     <And>
     <IsNull>
     <And>
     <IsNullable>
     <And>
     <IsCurRow<>[0]>
                           ,<Field> = row<Table>.Field<<VBType>?>("<Field>")
</If Loop>
                        };

            switch (sortOrder)
            {
    <If Loop>
	        <IsNotRelated>
	        <And>
	        <IsNotText>
	        <And>
	        <IsNotNText>
                case "<FieldU>_desc":
                    Query = Query.OrderByDescending(s => s.<FieldU>);
                    break;
                case "<FieldU>_asc":
                    Query = Query.OrderBy(s => s.<FieldU>);
                    break;
	        <IsRelated>
	        <And>
	        <IsNotText>
	        <And>
	        <IsNotNText>
                case "<FieldU>_desc":
                    Query = Query.OrderByDescending(s => s.<ROwner>_<RTable>.<RDisplayField>);
                    break;
                case "<FieldU>_asc":
                    Query = Query.OrderBy(s => s.<ROwner>_<RTable>.<RDisplayField>);
                    break;
    </If Loop>
                default:  // Name ascending 
                    Query = Query.OrderBy(s => s.<FieldU>);
                    break;
            }

            if (command == "Export") {
                GridView gv = new GridView();
                DataTable dt = new DataTable();
<Loop>
                dt.Columns.Add("<FieldC>", typeof(string));
</Loop>
                foreach (var item in Query)
                {
                    dt.Rows.Add(
    <If Loop>
        <IsNotRelated>
        <And>
        <IsCurRow=[0]>
                        item.<FieldU>
        <IsNotRelated>
        <And>
        <IsCurRow<>[0]>
                       ,item.<FieldU>
        <IsRelated>
        <And>
        <IsCurRow=[0]>
                        item.<ROwner>_<RTable>.<RDisplayField>
        <IsRelated>
        <And>
        <IsCurRow<>[0]>
                       ,item.<ROwner>_<RTable>.<RDisplayField>
    </If Loop>
                    );
                }
                gv.DataSource = dt;
                gv.DataBind();
                ExportData(Export, gv, dt);
            }

            int pageNumber = (page ?? 1);
            int? pageSZ = (Convert.ToInt32(Session["PageSize"]) == 0 ? 5 : Convert.ToInt32(Session["PageSize"]));
            return View(Query.ToPagedList(pageNumber, (pageSZ ?? 5)));
        }

        // GET: /<OwnerU>_<TableU>/Details/<id>
        public ActionResult Details(
    <If Loop>
      <IsPKey>
      <And>
      <IsNullable>
      <And>
      <IsCurRow=[0]>
                                      <VBType>? <FieldU>
      <IsPKey>
      <And>
      <IsNullable>
      <And>
      <IsCurRow<>[0]>
                                     ,<VBType>? <FieldU>
      <IsPKey>
      <And>
      <IsNotNullable>
      <And>
      <IsCurRow=[0]>
                                      <VBType> <FieldU>
      <IsPKey>
      <And>
      <IsNotNullable>
      <And>
      <IsCurRow<>[0]>
                                     ,<VBType> <FieldU>
    </If Loop>
                                   )
    <If Loop>
      <IsPKey>
        {
            if (
        <ExitLoop>
    </If Loop>
    <If Loop>
      <IsPKey>
      <And>
      <IsCurRow=[0]>
                    <FieldU> == null
      <IsPKey>
      <And>
      <IsCurRow<>[0]>
                 || <FieldU> == null
    </If Loop>
               )
    <If Loop>
      <IsPKey>
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
        <ExitLoop>
    </If Loop>

<If Loop>
     <IsRelated>
     <And>
     <IsMiscTag>
            A<MiscTag>dt<RTable> = <OwnerU>_<TableU>_<RTableU>Data.SelectAll();
     <IsRelated>
     <And>
     <IsNotMiscTag>
            dt<RTable> = <OwnerU>_<TableU>_<RTableU>Data.SelectAll();
</If Loop>

            <OwnerU>_<TableU> <OwnerU>_<TableU> = new <OwnerU>_<TableU>();
        <If Loop>
           <IsPKey>
            <OwnerU>_<TableU>.<FieldU> = <Convert>(<FieldU>);
        </If Loop>
        <If Loop>
           <IsNotPKey>
           <And>
           <IsIdentity>
            <OwnerU>_<TableU>.<FieldU> = <Convert>(<FieldU>);
        </If Loop>
        <If Loop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
            <OwnerU>_<TableU>.<FieldU> = <Convert>(<FieldU>);
           <ExitLoop>
        </If Loop>
            <OwnerU>_<TableU> = <OwnerU>_<TableU>Data.Select_Record(<OwnerU>_<TableU>);
<If Loop>
     <IsRelated>
     <And>
     <IsMiscTag>
     <And>
     <IsRDisplayField>
     <And>
     <IsCurRow=[0]>
            <OwnerU>_<TableU>.<RTable> = new <RTable>()
            {
                <RField> = (<VBType>)<OwnerU>_<TableU>.<RField>
               ,<RDisplayField> = (from DataRow A<MiscTag>row<RTable> in A<MiscTag>dt<RTable>.Rows
                      where <OwnerU>_<TableU>.<Field> == (int)A<MiscTag>row<RTable>["<RField>"]
                      select (<RDisplayFieldVBType>)A<MiscTag>row<RTable>["<RDisplayField>"]).FirstOrDefault()
            };
     <IsRelated>
     <And>
     <IsMiscTag>
     <And>
     <IsNotRDisplayField>
     <And>
     <IsCurRow=[0]>
            <OwnerU>_<TableU>.<RTable> = new <RTable>()
            {
                <RField> = (<VBType>)<OwnerU>_<TableU>.<RField>
            };
     <IsRelated>
     <And>
     <IsMiscTag>
     <And>
     <IsRDisplayField>
     <And>
     <IsCurRow<>[0]>
            <OwnerU>_<TableU>.<RTable> = new <RTable>()
            {
                <RField> = (<VBType>)<OwnerU>_<TableU>.<RField>
               ,<RDisplayField> = (from DataRow A<MiscTag>row<RTable> in A<MiscTag>dt<RTable>.Rows
                      where <OwnerU>_<TableU>.<Field> == (int)A<MiscTag>row<RTable>["<RField>"]
                      select (<RDisplayFieldVBType>)A<MiscTag>row<RTable>["<RDisplayField>"]).FirstOrDefault()
            };
     <IsRelated>
     <And>
     <IsMiscTag>
     <And>
     <IsNotRDisplayField>
     <And>
     <IsCurRow<>[0]>
            <OwnerU>_<TableU>.<RTable> = new <RTable>()
            {
                <RField> = (<VBType>)<OwnerU>_<TableU>.<RField>
            };
     <IsRelated>
     <And>
     <IsNotMiscTag>
     <And>
     <IsRDisplayField>
     <And>
     <IsCurRow=[0]>
            <OwnerU>_<TableU>.<RTable> = new <RTable>()
            {
                <RField> = (<VBType>)<OwnerU>_<TableU>.<RField>
               ,<RDisplayField> = (from DataRow row<RTable> in dt<RTable>.Rows
                      where <OwnerU>_<TableU>.<Field> == (int)row<RTable>["<RField>"]
                      select (<RDisplayFieldVBType>)row<RTable>["<RDisplayField>"]).FirstOrDefault()
            };
     <IsRelated>
     <And>
     <IsNotMiscTag>
     <And>
     <IsNotRDisplayField>
     <And>
     <IsCurRow=[0]>
            <OwnerU>_<TableU>.<RTable> = new <RTable>()
            {
                <RField> = (<VBType>)<OwnerU>_<TableU>.<RField>
            };
     <IsRelated>
     <And>
     <IsNotMiscTag>
     <And>
     <IsRDisplayField>
     <And>
     <IsCurRow<>[0]>
            <OwnerU>_<TableU>.<RTable> = new <RTable>()
            {
                <RField> = (<VBType>)<OwnerU>_<TableU>.<RField>
               ,<RDisplayField> = (from DataRow row<RTable> in dt<RTable>.Rows
                      where <OwnerU>_<TableU>.<Field> == (int)row<RTable>["<RField>"]
                      select (<RDisplayFieldVBType>)row<RTable>["<RDisplayField>"]).FirstOrDefault()
            };
     <IsRelated>
     <And>
     <IsNotMiscTag>
     <And>
     <IsNotRDisplayField>
     <And>
     <IsCurRow<>[0]>
            <OwnerU>_<TableU>.<RTable> = new <RTable>()
            {
                <RField> = (<VBType>)<OwnerU>_<TableU>.<RField>
            };
</If Loop>

    <If Loop>
      <IsPKey>
            if (<OwnerU>_<TableU> == null)
            {
                return HttpNotFound();
            }
            return View(<OwnerU>_<TableU>);
        }
        <ExitLoop>
    </If Loop>

        // GET: /<OwnerU>_<TableU>/Create
        public ActionResult Create()
        {
    <If Loop>
      <IsFKey>
        // ComboBox
      <ExitLoop>
    </If Loop>
    <If Loop>
    	<IsFKey>
            ViewData["<RFieldU>"] = new SelectList(<OwnerU>_<TableU>_<RTableU>Data.List(), "<RFieldU>", "<RDisplayFieldU>");
    </If Loop>

            return View();
        }

        // POST: /<OwnerU>_<TableU>/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include=
		            <If Loop>
                             <IsNotIdentity>
				   <SF[+ "," + ]>"<Field>"
		            </If Loop>
				  )] <OwnerU>_<TableU> <OwnerU>_<TableU>)
        {
            if (ModelState.IsValid)
            {
                bool bSucess = false;
                bSucess = <OwnerU>_<TableU>Data.Add(<OwnerU>_<TableU>);
                if (bSucess == true)
                {
                    return RedirectToAction("Index");
                }
                else
                {
                    ModelState.AddModelError("", "Can Not Insert");
                }
            }
    <If Loop>
      <IsFKey>
        // ComboBox
      <ExitLoop>
    </If Loop>
    <If Loop>
    	<IsFKey>
            ViewData["<RFieldU>"] = new SelectList(<OwnerU>_<TableU>_<RTableU>Data.List(), "<RFieldU>", "<RDisplayFieldU>", <OwnerU>_<TableU>.<FieldU>);
    </If Loop>

            return View(<OwnerU>_<TableU>);
        }

        // GET: /<OwnerU>_<TableU>/Edit/<id>
        public ActionResult Edit(
    <If Loop>
      <IsPKey>
      <And>
      <IsNullable>
      <And>
      <IsCurRow=[0]>
                                   <VBType>? <FieldU>
      <IsPKey>
      <And>
      <IsNullable>
      <And>
      <IsCurRow<>[0]>
                                  ,<VBType>? <FieldU>
      <IsPKey>
      <And>
      <IsNotNullable>
      <And>
      <IsCurRow=[0]>
                                   <VBType> <FieldU>
      <IsPKey>
      <And>
      <IsNotNullable>
      <And>
      <IsCurRow<>[0]>
                                  ,<VBType> <FieldU>
    </If Loop>
                                )
    <If Loop>
      <IsPKey>
        {
            if (
        <ExitLoop>
    </If Loop>
    <If Loop>
      <IsPKey>
      <And>
      <IsCurRow=[0]>
                    <FieldU> == null
      <IsPKey>
      <And>
      <IsCurRow<>[0]>
                 || <FieldU> == null
    </If Loop>
               )
    <If Loop>
      <IsPKey>
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
        <ExitLoop>
    </If Loop>

            <OwnerU>_<TableU> <OwnerU>_<TableU> = new <OwnerU>_<TableU>();
        <If Loop>
           <IsPKey>
            <OwnerU>_<TableU>.<FieldU> = <Convert>(<FieldU>);
        </If Loop>
        <If Loop>
           <IsNotPKey>
           <And>
           <IsIdentity>
            <OwnerU>_<TableU>.<FieldU> = <Convert>(<FieldU>);
        </If Loop>
        <If Loop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
            <OwnerU>_<TableU>.<FieldU> = <Convert>(<FieldU>);
           <ExitLoop>
        </If Loop>
            <OwnerU>_<TableU> = <OwnerU>_<TableU>Data.Select_Record(<OwnerU>_<TableU>);

    <If Loop>
      <IsPKey>
            if (<OwnerU>_<TableU> == null)
            {
                return HttpNotFound();
            }
        <ExitLoop>
    </If Loop>
    <If Loop>
      <IsFKey>
        // ComboBox
      <ExitLoop>
    </If Loop>
    <If Loop>
    	<IsFKey>
            ViewData["<RFieldU>"] = new SelectList(<OwnerU>_<TableU>_<RTableU>Data.List(), "<RFieldU>", "<RDisplayFieldU>", <OwnerU>_<TableU>.<FieldU>);
    </If Loop>

    <If Loop>
      <IsPKey>
            return View(<OwnerU>_<TableU>);
        }
        <ExitLoop>
    </If Loop>

        // POST: /<OwnerU>_<TableU>/Edit/<id>
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost, ActionName("Edit")]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(<OwnerU>_<TableU> <OwnerU>_<TableU>)
        {

            <OwnerU>_<TableU> o<OwnerU>_<TableU> = new <OwnerU>_<TableU>();
        <If Loop>
           <IsPKey>
            o<OwnerU>_<TableU>.<FieldU> = <Convert>(<OwnerU>_<TableU>.<FieldU>);
        </If Loop>
        <If Loop>
           <IsNotPKey>
           <And>
           <IsIdentity>
            o<OwnerU>_<TableU>.<FieldU> = <Convert>(<OwnerU>_<TableU>.<FieldU>);
        </If Loop>
        <If Loop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
            o<OwnerU>_<TableU>.<FieldU> = <Convert>(<OwnerU>_<TableU>.<FieldU>);
           <ExitLoop>
        </If Loop>
            o<OwnerU>_<TableU> = <OwnerU>_<TableU>Data.Select_Record(<OwnerU>_<TableU>);

            if (ModelState.IsValid)
            {
                bool bSucess = false;
                bSucess = <OwnerU>_<TableU>Data.Update(o<OwnerU>_<TableU>, <OwnerU>_<TableU>);
                if (bSucess == true)
                {
                    return RedirectToAction("Index");
                }
                else
                {
                    ModelState.AddModelError("", "Can Not Update");
                }
            }
    <If Loop>
      <IsFKey>
        // ComboBox
      <ExitLoop>
    </If Loop>
    <If Loop>
    	<IsFKey>
            ViewData["<RFieldU>"] = new SelectList(<OwnerU>_<TableU>_<RTableU>Data.List(), "<RFieldU>", "<RDisplayFieldU>", <OwnerU>_<TableU>.<FieldU>);
    </If Loop>

            return View(<OwnerU>_<TableU>);
        }

        // GET: /<OwnerU>_<TableU>/Delete/<id>
        public ActionResult Delete(
    <If Loop>
      <IsPKey>
      <And>
      <IsNullable>
      <And>
      <IsCurRow=[0]>
                                     <VBType>? <FieldU>
      <IsPKey>
      <And>
      <IsNullable>
      <And>
      <IsCurRow<>[0]>
                                    ,<VBType>? <FieldU>
      <IsPKey>
      <And>
      <IsNotNullable>
      <And>
      <IsCurRow=[0]>
                                     <VBType> <FieldU>
      <IsPKey>
      <And>
      <IsNotNullable>
      <And>
      <IsCurRow<>[0]>
                                    ,<VBType> <FieldU>
    </If Loop>
                                  )
    <If Loop>
      <IsPKey>
        {
            if (
        <ExitLoop>
    </If Loop>
    <If Loop>
      <IsPKey>
      <And>
      <IsCurRow=[0]>
                    <FieldU> == null
      <IsPKey>
      <And>
      <IsCurRow<>[0]>
                 || <FieldU> == null
    </If Loop>
               )
    <If Loop>
      <IsPKey>
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
        <ExitLoop>
    </If Loop>

<If Loop>
     <IsRelated>
     <And>
     <IsMiscTag>
            A<MiscTag>dt<RTable> = <OwnerU>_<TableU>_<RTableU>Data.SelectAll();
     <IsRelated>
     <And>
     <IsNotMiscTag>
            dt<RTable> = <OwnerU>_<TableU>_<RTableU>Data.SelectAll();
</If Loop>

            <OwnerU>_<TableU> <OwnerU>_<TableU> = new <OwnerU>_<TableU>();
        <If Loop>
           <IsPKey>
            <OwnerU>_<TableU>.<FieldU> = <Convert>(<FieldU>);
        </If Loop>
        <If Loop>
           <IsNotPKey>
           <And>
           <IsIdentity>
            <OwnerU>_<TableU>.<FieldU> = <Convert>(<FieldU>);
        </If Loop>
        <If Loop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
            <OwnerU>_<TableU>.<FieldU> = <Convert>(<FieldU>);
           <ExitLoop>
        </If Loop>
            <OwnerU>_<TableU> = <OwnerU>_<TableU>Data.Select_Record(<OwnerU>_<TableU>);
<If Loop>
     <IsRelated>
     <And>
     <IsMiscTag>
     <And>
     <IsRDisplayField>
     <And>
     <IsCurRow=[0]>
            <OwnerU>_<TableU>.<RTable> = new <RTable>()
            {
                <RField> = (<VBType>)<OwnerU>_<TableU>.<RField>
               ,<RDisplayField> = (from DataRow A<MiscTag>row<RTable> in A<MiscTag>dt<RTable>.Rows
                      where <OwnerU>_<TableU>.<Field> == (int)A<MiscTag>row<RTable>["<RField>"]
                      select (<RDisplayFieldVBType>)A<MiscTag>row<RTable>["<RDisplayField>"]).FirstOrDefault()
            };
     <IsRelated>
     <And>
     <IsMiscTag>
     <And>
     <IsNotRDisplayField>
     <And>
     <IsCurRow=[0]>
            <OwnerU>_<TableU>.<RTable> = new <RTable>()
            {
                <RField> = (<VBType>)<OwnerU>_<TableU>.<RField>
            };
     <IsRelated>
     <And>
     <IsMiscTag>
     <And>
     <IsRDisplayField>
     <And>
     <IsCurRow<>[0]>
            <OwnerU>_<TableU>.<RTable> = new <RTable>()
            {
                <RField> = (<VBType>)<OwnerU>_<TableU>.<RField>
               ,<RDisplayField> = (from DataRow A<MiscTag>row<RTable> in A<MiscTag>dt<RTable>.Rows
                      where <OwnerU>_<TableU>.<Field> == (int)A<MiscTag>row<RTable>["<RField>"]
                      select (<RDisplayFieldVBType>)A<MiscTag>row<RTable>["<RDisplayField>"]).FirstOrDefault()
            };
     <IsRelated>
     <And>
     <IsMiscTag>
     <And>
     <IsNotRDisplayField>
     <And>
     <IsCurRow<>[0]>
            <OwnerU>_<TableU>.<RTable> = new <RTable>()
            {
                <RField> = (<VBType>)<OwnerU>_<TableU>.<RField>
            };
     <IsRelated>
     <And>
     <IsNotMiscTag>
     <And>
     <IsRDisplayField>
     <And>
     <IsCurRow=[0]>
            <OwnerU>_<TableU>.<RTable> = new <RTable>()
            {
                <RField> = (<VBType>)<OwnerU>_<TableU>.<RField>
               ,<RDisplayField> = (from DataRow row<RTable> in dt<RTable>.Rows
                      where <OwnerU>_<TableU>.<Field> == (int)row<RTable>["<RField>"]
                      select (<RDisplayFieldVBType>)row<RTable>["<RDisplayField>"]).FirstOrDefault()
            };
     <IsRelated>
     <And>
     <IsNotMiscTag>
     <And>
     <IsNotRDisplayField>
     <And>
     <IsCurRow=[0]>
            <OwnerU>_<TableU>.<RTable> = new <RTable>()
            {
                <RField> = (<VBType>)<OwnerU>_<TableU>.<RField>
            };
     <IsRelated>
     <And>
     <IsNotMiscTag>
     <And>
     <IsRDisplayField>
     <And>
     <IsCurRow<>[0]>
            <OwnerU>_<TableU>.<RTable> = new <RTable>()
            {
                <RField> = (<VBType>)<OwnerU>_<TableU>.<RField>
               ,<RDisplayField> = (from DataRow row<RTable> in dt<RTable>.Rows
                      where <OwnerU>_<TableU>.<Field> == (int)row<RTable>["<RField>"]
                      select (<RDisplayFieldVBType>)row<RTable>["<RDisplayField>"]).FirstOrDefault()
            };
     <IsRelated>
     <And>
     <IsNotMiscTag>
     <And>
     <IsNotRDisplayField>
     <And>
     <IsCurRow<>[0]>
            <OwnerU>_<TableU>.<RTable> = new <RTable>()
            {
                <RField> = (<VBType>)<OwnerU>_<TableU>.<RField>
            };
</If Loop>

    <If Loop>
      <IsPKey>
            if (<OwnerU>_<TableU> == null)
            {
                return HttpNotFound();
            }
            return View(<OwnerU>_<TableU>);
        }
        <ExitLoop>
    </If Loop>

        // POST: /<OwnerU>_<TableU>/Delete/<id>
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(
    <If Loop>
      <IsPKey>
      <And>
      <IsNullable>
      <And>
      <IsCurRow=[0]>
                                            <VBType>? <FieldU>
      <IsPKey>
      <And>
      <IsNullable>
      <And>
      <IsCurRow<>[0]>
                                            ,<VBType>? <FieldU>
      <IsPKey>
      <And>
      <IsNotNullable>
      <And>
      <IsCurRow=[0]>
                                            <VBType> <FieldU>
      <IsPKey>
      <And>
      <IsNotNullable>
      <And>
      <IsCurRow<>[0]>
                                            ,<VBType> <FieldU>
    </If Loop>
                                            )
        {

            <OwnerU>_<TableU> <OwnerU>_<TableU> = new <OwnerU>_<TableU>();
        <If Loop>
           <IsPKey>
            <OwnerU>_<TableU>.<FieldU> = <Convert>(<FieldU>);
        </If Loop>
        <If Loop>
           <IsNotPKey>
           <And>
           <IsIdentity>
            <OwnerU>_<TableU>.<FieldU> = <Convert>(<FieldU>);
        </If Loop>
        <If Loop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
            <OwnerU>_<TableU>.<FieldU> = <Convert>(<FieldU>);
           <ExitLoop>
        </If Loop>
            <OwnerU>_<TableU> = <OwnerU>_<TableU>Data.Select_Record(<OwnerU>_<TableU>);

            bool bSucess = false;
            bSucess = <OwnerU>_<TableU>Data.Delete(<OwnerU>_<TableU>);
            if (bSucess == true)
            {
                return RedirectToAction("Index");
            }
            else
            {
                ModelState.AddModelError("", "Can Not Delete");
            }
            return null;
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }

        private static List<SelectListItem> GetFields(String select)
        {
            List<SelectListItem> list = new List<SelectListItem>();
	<iVarA=[1]>
<If Loop>
   	<IsNotText>
   	<And>
   	<IsNotNText>
            SelectListItem Item<iVarA> = new SelectListItem { Text = "<FieldC>", Value = "<FieldC>" };
        <iVarA=+[1]>
</If Loop>

	<iVarA=[1]>
<If Loop>
   	<IsNotText>
   	<And>
   	<IsNotNText>
            <SF[else ]>if (select == "<FieldC>") { Item<iVarA>.Selected = true; }
        <iVarA=+[1]>
</If Loop>

	<iVarA=[1]>
<If Loop>
   	<IsNotText>
   	<And>
   	<IsNotNText>
            list.Add(Item<iVarA>);
        <iVarA=+[1]>
</If Loop>

            return list.ToList();
        }

        private void ExportData(String Export, GridView gv, DataTable dt)
        {
            if (Export == "Pdf")
            {
                PDFform pdfForm = new PDFform(dt, "<Head>", "Many");
                Document document = pdfForm.CreateDocument();
                PdfDocumentRenderer renderer = new PdfDocumentRenderer(true);
                renderer.Document = document;
                renderer.RenderDocument();

                MemoryStream stream = new MemoryStream();
                renderer.PdfDocument.Save(stream, false);

                Response.Clear();
                Response.AddHeader("content-disposition", "attachment;filename=" + "Report.pdf");
                Response.ContentType = "application/Pdf.pdf";
                Response.BinaryWrite(stream.ToArray());
                Response.Flush();
                Response.End();
            }
            else
            {
                Response.ClearContent();
                Response.Buffer = true;
                if (Export == "Excel")
                {
                    Response.AddHeader("content-disposition", "attachment;filename=" + "Report.xls");
                    Response.ContentType = "application/Excel.xls";
                }
                else if (Export == "Word")
                {
                    Response.AddHeader("content-disposition", "attachment;filename=" + "Report.doc");
                    Response.ContentType = "application/Word.doc";
                }
                Response.Charset = "";
                StringWriter sw = new StringWriter();
                HtmlTextWriter htw = new HtmlTextWriter(sw);
                gv.RenderControl(htw);
                Response.Output.Write(sw.ToString());
                Response.Flush();
                Response.End();
            }
        }

    }
}
